package com.dang.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dang.dao.ReceiveAddressDAO;
import com.dang.model.ReceiveAddress;
import com.dang.util.DBConnection;

public class ReceiveAddressDAOImp implements ReceiveAddressDAO{
	private static String FIND_BY_ID="select * from d_receive_address where id=?";
	private static String FIND_BY_USER_ID="select * from d_receive_address where user_id=?";
	private static String SAVE="insert into d_receive_address(id,user_id,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?)";
	
	public ReceiveAddress findById(int id)throws Exception{
		PreparedStatement prep=null;
		Connection conn=null;
		ReceiveAddress addr=null;
		ResultSet rst=null;
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rst=prep.executeQuery();
			while(rst.next()){
				addr=new ReceiveAddress();
				addr.setId(rst.getInt(id));
				addr.setUserId(rst.getInt("user_id"));
				addr.setFullAddress(rst.getString("full_address"));
				addr.setReceiveName(rst.getString("receive_name"));
				addr.setPostalCode(rst.getString("postal_code"));
				addr.setMobile(rst.getString("mobile"));
				addr.setPhone(rst.getString("phone"));
			}
		}finally{
			DBConnection.close(rst,prep, conn);
		}
		return addr;
	}
	
	public List<ReceiveAddress> findByUserId(int userId) throws Exception{
		PreparedStatement prep=null;
		Connection conn=null;
		ReceiveAddress addr=null;
		ResultSet rst=null;
		List<ReceiveAddress> pros = new ArrayList<ReceiveAddress>();
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_USER_ID);
			prep.setInt(1,userId);
			rst=prep.executeQuery();
			while(rst.next()){
				addr = new ReceiveAddress();
				addr.setId(rst.getInt("id"));
				addr.setUserId(rst.getInt("user_id"));
				addr.setFullAddress(rst.getString("full_address"));
				addr.setReceiveName(rst.getString("receive_name"));
				addr.setPostalCode(rst.getString("postal_code"));
				addr.setMobile(rst.getString("mobile"));
				addr.setPhone(rst.getString("phone"));
				
				pros.add(addr);
			}
		}finally{
			DBConnection.close(rst,prep, conn);
		}
		return pros;
	}
	
	public void save(ReceiveAddress addr) throws Exception{
		PreparedStatement prep=null;
		Connection conn=null;
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(SAVE,Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, addr.getId());
			prep.setInt(2, addr.getUserId());
			prep.setString(3, addr.getReceiveName());
			prep.setString(4, addr.getFullAddress());
			prep.setString(5, addr.getPostalCode());
			prep.setString(6, addr.getMobile());
			prep.setString(7, addr.getPhone());
			prep.executeUpdate();
			ResultSet rst=prep.getGeneratedKeys();
			rst.next();
			int id=rst.getInt(1);
			addr.setId(id);
		}finally{
			DBConnection.close(prep, conn);
		}
	}
}	
